#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""
import hashlib
import time
import base64
import os
import re
import sys
import json
import pandas
import chardet
import graphene
import config
from common.view import BaseApi
from res.lijiacai.utils import tool


class RecommandStations(BaseApi, tool.Recommand):
    name = "RecommandStations"
    description = "智能调度推荐站点"
    es_cluster = config.es_cluster
    redis_conf = config.redis_conf
    station_url = config.station_url
    station_distance_dataset = config.station_distance_dataset

    class Argument:
        token = graphene.String(description="认证token", required=True)
        charge_station_id = graphene.String(description="充电站ID", required=True)
        X1 = graphene.String(description="策略中的对应站点充电车辆数低阈值，默认0", default=0)
        X2 = graphene.String(description="策略中的对应站点需调入车辆数低阈值，默认0", default=0)
        X3 = graphene.String(description="策略中的对应站点可用车位数低阈值，默认1", default=1)

    class Return:
        class RecommandStations1(graphene.ObjectType):
            station_id = graphene.String(description="站点id")
            order_num = graphene.String(description="预测订单数")
            return_num = graphene.String(description="预测还车数")
            into_num = graphene.String(description="需调入车辆数")
            distance = graphene.String(description="导航距离")
            duration = graphene.String(description="时间")

        rows = graphene.List(RecommandStations1, description="推荐列表")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        token = self.arguments.get("condition", {}).get("token")
        self.deal_token(token)
        charge_station_id = self.arguments.get("condition", {}).get("charge_station_id")
        X1 = self.arguments.get("condition", {}).get("X1", 0)
        X2 = self.arguments.get("condition", {}).get("X2", 0)
        X3 = self.arguments.get("condition", {}).get("X3", 1)
        rows = self.go(charge_station_id=charge_station_id, X1=X1, X2=X2, X3=X3, max_km=10000,
                       high_period=[7, 8, 9, 17, 18],
                       max_time=1800)

        return {"rows": rows}


class ApplyPutIn(BaseApi, tool.Recommand):
    name = "ApplyPutIn"
    description = "申请站点调入车辆"

    redis_conf = config.redis_conf
    station_url = config.station_url

    class Argument:
        token = graphene.String(description="认证token", required=True)
        apply_station_id = graphene.String(description="申请调入站点ID", required=True)
        apply_license = graphene.String(description="申请调入的车牌号", required=True)

    class Return:
        succ = graphene.Boolean(description="操作状态")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        token = self.arguments.get("condition", {}).get("token")
        apply_station_id = self.arguments.get("condition", {}).get("apply_station_id")
        apply_license = self.arguments.get("condition", {}).get("apply_license")
        self.deal_token(token)
        if self.redis_client.get(apply_station_id + "_" + apply_license):
            raise Exception("ERROR:已被申请调入")
        self.redis_client.set(apply_station_id + "_" + apply_license, "", ex=60 * 60 * 3, px=None, nx=False, xx=False)
        return {"succ": True}


class ApplyPutOut(BaseApi, tool.Recommand):
    name = "ApplyPutOut"
    description = "撤销站点调入车辆或完成站点调入"

    redis_conf = config.redis_conf
    station_url = config.station_url

    class Argument:
        token = graphene.String(description="认证token", required=True)
        apply_station_id = graphene.String(description="撤销调入站点ID", required=True)
        apply_license = graphene.String(description="撤销调入的车牌号", required=True)

    class Return:
        succ = graphene.Boolean(description="操作状态")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        token = self.arguments.get("condition", {}).get("token")
        apply_station_id = self.arguments.get("condition", {}).get("apply_station_id")
        apply_license = self.arguments.get("condition", {}).get("apply_license")
        self.deal_token(token)
        try:
            self.redis_client.delete(apply_station_id + "_" + apply_license)
        except:
            raise Exception("ERROR:撤销失败，不存在撤销内容")
        return {"succ": True}
